window.onload=function(){
	$(".devicelist").css("backgroundColor","#224247").siblings().css("backgroundColor","#365d62");
	//查询搜索下拉框
	$(".deviceCheckBox").click(function(event){
		event.stopPropagation();
		$(".deviceCheckDropBox").show();
		$(".deviceCheckDropBox a").unbind('click').click(function(){
			$(".deviceCheckBox").html($(this).html());
			$(".deviceCheckDropBox").hide();
		});
		$("body").unbind('click').click(function(){
			$(".deviceCheckDropBox").hide();
		});
	});
	$(".searchList > a").click(function(){
		var device_code = $(".deviceCodeBox").val();
		var status = $(".deviceCheckBox").html();
		if(device_code=="%"){
			hint("非法输入，请正确输入编号！");
		}else{
			window.location.href='/admin/device/index?condition='+device_code+'&status='+status;
		}

	});
	//充值
	$(".payFluxBtn").click(function(event){
		var reg =/^[0-9]*$/;
		event.stopPropagation();
		$(".labelBox").html("");
		$(".payBox input").val("");
		var thisId=$(this);
		thisId.css("visibility","hidden").siblings(".modeBox").show();
		//获取当前设备编号前两位字符串
		var deviceCodeHead=thisId.parent().siblings(".deviceCodeCell").html();
		var CodeHead=deviceCodeHead.substring(0,2);
		//计量模式续费
		thisId.siblings(".modeBox").children(".meterageMode").unbind("click").click(function(event){
			event.stopPropagation();
			//获取用户信息
			$(".getDeviceCode").html(thisId.parent().siblings(".deviceCodeCell").html());
			$(".getUserList").html(thisId.parent().siblings(".userListCell").html());
			$(".getSurplusFlux").html(thisId.parent().siblings(".residueFluxCell").html());
			$(".getSurplusDays").html(thisId.parent().siblings(".residueDaysCell").html());
			//显示充值弹框
			layui.use('layer', function(){
			  	var layer = layui.layer;
			  	layer.open({
					type: 1, 
					title: ['充值操作', 'font-size:18px;'],
					area: ['800px', '500px'],
				  	content: $(".meterageM")
				});
			});
			DropinputFn(".payBoxInput",".moneyNumDrop",".fluxNumBox");//充值数值下拉框
			$("body").unbind("click").click(function(){
				thisId.css("visibility","visible").siblings(".modeBox").hide();
			})
			//取消充值
			$(".deviceChargeNoBtn").unbind('click').click(function(){
				window.location.reload();
			})
			//确定充值
			
			var oldFluxNumCell=parseInt(thisId.parent().siblings(".residueFluxCell").html());
			$(".okBtn").unbind('click').click(function(e){
				if(oldFluxNumCell+parseInt($(".meterageM .fluxNumBox").html())>65000){
					$(".meterageM .payMoneyLabelBox").html("单次计量续费数值过大，请重新输入！");
					$(".meterageM .payBoxInput").val("");
					$(".meterageM .fluxNumBox").html("");
				}else{
					if(CodeHead=="20"){//旧协议充值接口
						if(!reg.test(parseInt($(".meterageM .payBoxInput").val()))||parseInt($(".meterageM .payBoxInput").val())==0){
							$(".meterageM .payMoneyLabelBox").html("请输入正整数！");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.parent().siblings().eq(0).children().val(),
									money:$(".meterageM .payBoxInput").val(),
									flow:Math.round($(".meterageM .payBoxInput").val()/$(".meterageM .fluxPrice").val()),
									remark:$(".meterageM .Add-ons_txt").val()
								},
								success:function(data){
									if(data.status.errCode==0){
										//清空
										$(".meterageM .payBoxInput").val("");
										$(".meterageM .fluxNumBox").html("");
										$(".meterageM .payMoneyLabelBox").html("操作成功！"+data.status.message);
									}else{
										$(".meterageM .payMoneyLabelBox").html(data.status.message);
									}
								}
							})
						}
					}else if(CodeHead=="21"){//新协议充值接口
						if(!reg.test($(".meterageM .payBoxInput").val())||parseInt($(".meterageM .payBoxInput").val())==0){
							$(".meterageM .payMoneyLabelBox").html("请输入正整数！");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge/mode",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.parent().siblings().eq(0).children().val(),
									mode:2,
									money:$(".meterageM .payBoxInput").val(),
									quantity:Math.round($(".meterageM .payBoxInput").val()/$(".meterageM .fluxPrice").val()),
									remark:$(".meterageM .Add-ons_txt").val()
								},
								success:function(data){
									if(data.status.errCode==0){
										//清空
										$(".meterageM .payBoxInput").val("");
										$(".meterageM .fluxNumBox").html("");
										$(".meterageM .payMoneyLabelBox").html("操作成功！"+data.status.message);
									}else{
										$(".meterageM .payMoneyLabelBox").html(data.status.message);
									}
								}
							})
						}
					}else{
						hint("设备码非法！");
					}
				}
			});			
		})
		//套餐模式续费
		thisId.siblings(".modeBox").children(".packageMode").unbind("click").click(function(event){
			event.stopPropagation();
			if(CodeHead=="20"){
				hint("该设备为第一批出厂设备，不具备套餐模式续费！");
			}else if(CodeHead=="21"){
				//获取用户信息
				$(".getDeviceCode").html(thisId.parent().siblings(".deviceCodeCell").html());
				$(".getUserList").html(thisId.parent().siblings(".userListCell").html());
				$(".getSurplusFlux").html(thisId.parent().siblings(".residueFluxCell").html());
				//显示充值弹框
				layui.use('layer', function(){
				  	var layer = layui.layer;
				  	layer.open({
						type: 1, 
						title: ['充值操作', 'font-size:18px;'],
						area: ['800px', '500px'],
					  	content: $(".packageM")
					}); 
				});
				DropinputFn(".payBoxInput",".moneyNumDrop",".fluxNumBox");//充值数值下拉框
				//取消充值
				$(".deviceChargeNoBtn").unbind('click').click(function(){
					window.location.reload();
				})
				//确定充值
				var oldDaysNumCell=parseInt(thisId.parent().siblings(".residueDaysCell").html());
				$(".okBtn").unbind('click').click(function(e){
					if(oldDaysNumCell+parseInt($(".packageM .payBoxInput").val())>10000){
						$(".packageM .payBoxInput").val("");
						$(".packageM .payMoneyLabelBox").html("单次套餐续费数值过大，请重新输入！");
					}else{
						if(!reg.test($(".packageM .payBoxInput").val())||parseInt($(".packageM .payBoxInput").val())==0){
							$(".packageM .payMoneyLabelBox").html("续费天数请输入正整数！");
							e.preventDefault();
						}else{
							$.ajax({
								url:"/admin/api/device/recharge/mode",
								type:"post",
								dataType:"json",
								data:{
									device_id:thisId.parent().siblings().eq(0).children().val(),
									remark:$(".packageM .Add-ons_txt").val(),
									mode:1,
									quantity:parseInt($(".packageM .payBoxInput").val())
								},
								success:function(data){
									if(data.status.errCode==0){
										//清空
										$(".packageM .payBoxInput").val("");
										$(".packageM .payMoneyLabelBox").html("操作成功！"+data.status.message);
									}else{
										$(".packageM .payMoneyLabelBox").html(data.status.message);
									}
								}
							})
						}
					}
				});
				$("body").unbind("click").click(function(){
					thisId.css("visibility","visible").siblings(".modeBox").hide();
				})
			}else{
				hint("设备编号非法");
			}	
		})
		
		$.ajax({
			url:"/admin/api/device/install-address",
			type:"post",
			dataType:"json",
			data:{
				device_code:thisId.parent().siblings(".deviceCodeCell").html()
			},
			success:function(data){
				if(data.status.errCode==0){
					$(".getAddress").html(data.data.install_address);
				}
			}
		});
		
	});
	//开启关闭
	$(".switch").click(function(e){
		$(".labelBox").html("");
		if($(this).html()=="停机中"||$(this).html()=="开启中"){
			e.stopPropagation();
		}else{
			var _this=$(this);
			layui.use('layer', function(){
			  	var layer = layui.layer;
			  	layer.open({
					type: 1, 
					title: ['停机操作', 'font-size:18px;'],
					area: ['500px', '300px'],
				  	content: $(".switchBox")
				}); 
			});
			$(".switchBox > p >span").html($(this).html());
			$(".switchBox > div >span").html($(this).html());
			$(".getDeviceCode").html($(this).parent().siblings().eq(1).html());
			//close按钮
			$(".layui-layer-close").unbind('click').click(function(){
				window.location.href="/admin/device/index";
			})
			//取消按钮
			$('.noSwitch').unbind('click').click(function(){
				window.location.reload();
			})
			//确定按钮
			$(".okSwitch").unbind('click').click(function(){
				if(_this.html()=="停机"){
					var command ="halt";
				}else{
					var command ="stop_halt";
				}
				$.ajax({
					url:"/admin/api/device/halt",
					type:"post",
					dataType:"json",
					data:{
						command:command,
						device_code:_this.parent().siblings().eq(1).html()
					},
					success:function(data){
						if(data.status.errCode == 0){
							$(".switchLabelBox").html("操作成功！");
							if(_this.html()=="停机"){
								_this.html("停机中");
							}else{
								_this.html("开启中");
							}
							setTimeout(function(){
								window.location.reload();
							},2000);
						}else {
							$(".switchLabelBox").html(data.status.message);
							setTimeout(function(){
								window.location.reload();
							},2000);
						}
					}
				});
			});
		}
	});
	
	
}